<?php
class ModelLogin extends CI_Model {

    var $username = '';
	var $admin_id = '';
	var $password = '';
	
	function __construct()
    {
        parent::__construct();
    }
	
   /**
    * Function authenticateUser($data)
    * used For fetching data
    * @param $data array passing post value
    * perpose to check authenticate user data
    */
    
	function authenticateUser($data)
    {
        $this->username   	= $data['username'];
        $this->password 	=  $data['password'];
	
		$login = false;
		
		$query = "SELECT id,username,full_name FROM ".ADMIN." 
		          WHERE username = '".$this->username."' AND password = '".md5($this->password)."' AND  status = 'Active' ";
		$rs = $this->db->query($query);
		
		if ($rs->num_rows() >0 ){
			$row = $rs->row();
			$login = true;			
		}
		
		if($login == true){
		    	$this->nsession->set_userdata('user_session_id', $row->id);
			$this->nsession->set_userdata('user_session_username', $row->username);
			$this->nsession->set_userdata('user_session_fname', $row->full_name);
			
			return true;
		}else{
		    
		$query = "SELECT id,username,full_name FROM ".ADMIN." 
		          WHERE username = '".$this->username."' AND password = '".md5($this->password)."' AND  status = 'Inactive' ";
		$rs = $this->db->query($query);
		if ($rs->num_rows() >0 ){
		    $this->nsession->set_userdata('user_status', 'inactive');
		}
			return false;
		}
    }
	
   /**
    * Function isValidUsername($data).
    * Function used check valid user name
    * @param $data array passing post value
    * @author Biplab Biswas
    * perpose to check username
    */
    
	function isValidUsername($data){
        $this->username   	= $data['username'];
		$query = "SELECT id FROM ".ADMIN." 
				  WHERE username = '".$this->username."' AND  status ='Active'";
		
		$rs = $this->db->query($query);
		if ($rs->num_rows() >0 )
		{
			$row = $rs->row();
			$id = $row->id;
			return $id;			
		}else{
			return false;
		}
    }
	
   /**
    * Function updateAdminEmail($id,$data).
    * Function used check valid email
    * @param $data array passing post value
    * @param $id int user id.
    * @author Biplab Biswas
    * perpose to check user email
    */
    
	function updateAdminEmail($id,$data)
	{
		$this->email_address	= $data['email_address'];
		$query = "UPDATE ".ADMIN." 
		          SET email_address ='".$this->email_address."' 
		          WHERE id ='".$id."'";
		
		$rs = $this->db->query($query);
		
		if($this->db->affected_rows()){
			return true;
		}else{
			return false;
		}
	}
	
	/**
    * Function updateAdminEmail($id,$data).
    * Function used check valid email
    * @param $data array passing post value
    * @param $id int user id.
    * @author Biplab Biswas
    * perpose to check user email
    */
	
	function profileUpdate($id,$data)
	{
		$full_name			= $data['full_name'];
		$email_address		= $data['email_address'];
		
		$query = "UPDATE ".ADMIN." SET full_name 				= '".$full_name."',
		                               email_address 			= '".$email_address."' WHERE id ='".$id."'";
		
		$rs = $this->db->query($query);
		
		if($this->db->affected_rows()){
			return $this->db->affected_rows();
		}else{
			return $this->db->affected_rows();
		}
	}
	
	
	/**
    * Function getEmail($id).
    * Function for get admin email from user id
    * @param $id int user id.
    * @author Biplab Biswas
    * perpose to check user email
    */
	
	function getEmail($id)
	{
		$query = "SELECT email_address FROM ".ADMIN." WHERE id ='".$id."'";
		$rs = $this->db->query($query);
		
		if($rs->num_rows() > 0)
		{
			$row = $rs->row();
			$email = $row->email_address;
			return $email;
		}else{
			return "";
		}
	}
		
	/**
    * Function updateAdminPass($id,$data).
    * Function for update admin password
    * @param $id int user id.
    * @param $data array passing post value
    * perpose to check user email
    */
	
	function updateAdminPass($id,$new_admin_pwd)
	{
		
		$query = "UPDATE ".ADMIN." SET password = '".md5($new_admin_pwd)."' WHERE id ='".$id."'";
		$rs = $this->db->query($query);

		echo $this->db->affected_rows(); 
		
		if($this->db->affected_rows())
		{
			return $this->db->affected_rows();
		}else{
			return $this->db->affected_rows();
		}
	}
	
	/**
    * Function getProfileData($id = 0).
    * Function for get user profile
    * @param $id int user id.
    * @author Biplab Biswas
    * perpose to check user email
    */
	
	function getProfileData($id = 0)
	{
		$sql="SELECT `id`,`username`,`password`,`email_address`,`full_name`,`status`,`date_added` FROM ".ADMIN." WHERE id = ".$id." ";
		$recordSet = $this->db->query($sql);
		//pr($recordSet->fields);
		$rs = false;
		if ($recordSet->num_rows() > 0)
		{
			$rs = array();
			$isEscapeArr = array();
			$row = $recordSet->row_array();
			foreach($row as $key =>$val)
			{
				if(!in_array($key,$isEscapeArr))
					$recordSet->fields[$key] = outputEscapeString($val);
			}
			$rs[] = $recordSet->fields;
		}else{
			return false;
		}
		return $rs;		
	}
	
   /**
	* Function valideOldPassword(&$data).
    * @author Biplab Biswas
	* @param $data Array passing Post Values
	* purpose to check old passwod
	*/
	
	function valideOldPassword(&$data)
	{	
		$oldpassword = $data['oldpassword'];
		$id = $this->nsession->userdata('user_session_id');
		$admin_pwd_sql = "SELECT count(id) as CNT FROM ".ADMIN." WHERE id ='".$id."' AND  password ='".md5($oldpassword)."'";
		$recordSet = $this->db->query($admin_pwd_sql);
		$rs = false;		
		if($recordSet->num_rows() > 0){
			$rs = array();
			$isEscapeArr = array();
			$row = $recordSet->row_array();
			
			if($row['CNT']>0){
				return true;
			}else{
				return false;
			}
		}else{
			   return false;
		}
	}
	
	function get_dashboard(){
	    // GET DATA FROM GET/POST  OR   SESSION ====================
		
		$orderBy = "ORDER BY order_date DESC";
		
		$sql=" SELECT id,order_date,payment_type,member_id,billing_fname, billing_lname, billing_email, grand_total, payment_status,transaction_id,payment_date,order_status, transaction_msg FROM ".ORDERMASTER." WHERE 1  $orderBy  LIMIT 0,10 " ;

		$recordSet = $this->db->query($sql);
		$rs = false;
		if ($recordSet->num_rows() > 0)
        {
           	$rs = array();
			$isEscapeArr = array();
			foreach ($recordSet->result_array() as $row)
				{
					foreach($row as $key=>$val){
						if(!in_array($key,$isEscapeArr)){
							$recordSet->fields[$key] = outputEscapeString($val);
							if($key=='order_status'){
								$recordSet->fields['order_status_val'] = $this->getNameTable(DEPARTMENT,'dep_name','dep_code',$val);
							}
						}
					}
					$rs[] = $recordSet->fields;
				}
        }
		else
		{
			return false;
		}
		

		return $rs;
	    
	    
	    
	    
	}
	function getNameTable($table,$col,$field,$value)
	{
		$query="SELECT ".$col." FROM ".$table." where ".$field."='".$value."' ";
		$recordSet = $this->db->query($query);
		if($recordSet->num_rows() > 0)
		{
			$row = $recordSet->row_array();
			return $row[$col];
		}
		else
		{
			return "";
		}
	}
	
	
	function get_count($table_name,$fieldname,$month=''){
	    
	    if($month == 'last'){
	    $month =  date('m') - 1;
		$year = date('Y');
		if(strlen($month) == 1){
		    
		    $monthL = '0'.$month;
		}else{
		    $monthL = $month;
		}
		if($month == 0){
		    
		    $monthL = 12;
		}
	    }else{
		    $month =  date('m');
		    $year = date('Y');
		    if(strlen($month) == 1){
		    
		     $monthL = '0'.$month;
		     
		    }else{
			$monthL = $month;
		    }
	    }
		$date = $year.'-'.$monthL.'%';
	    
	    
	      //  echo $month; die;
	    $query="SELECT * FROM ".$table_name." where $fieldname LIKE '".$date."' ";
	 
		$recordSet = $this->db->query($query);
		
		if($recordSet->num_rows() > 0)
		{
			$row = $recordSet->num_rows();
			return $row;
		}
		else
		{
			return "";
		}
	    
	}
		


}

?>